# 使用
# 桌面建个文件夹，在文件夹里新建文本文件，重命名为 appicon.sh 脚本文件，将此段代码拷入其中
# 将要转化的 icon.png 图标放在和 appicon.sh 脚本同级目录(刚才新建的文件夹内)(注：png 图片不能为压缩过以后的图片，且最好大于 1024)
# 终端 cd 到 appicon.sh 所在目录(刚才新建的文件夹的目录)
# 执行 sh appicon.sh
# 将生成的 AppIcon.appiconset 文件夹覆盖至项目中即可

icon_path=$PWD'/config/baseConfig/icon.png'
if [ ! -f "$icon_path" ]; then
    echo $icon_path"不存在！"
    exit 0
fi

echo $PWD

iPhoneIconWithSize() {
    iphone_array=($(echo $1 | tr 'x' ' '))
    iphone_left="${iphone_array[0]}"
    iphone_right="${iphone_array[1]}"
    iphone_size=$(expr $iphone_left \* $iphone_right)
    iphone_name=icon-${iphone_left}.png
    if [ $iphone_right = '1' ]; then
        iphone_name=icon-${iphone_left}.png
    else
        iphone_name=icon-${iphone_left}@${iphone_right}x.png
    fi
    sips -Z $iphone_size $icon_path --out ./AppIcon.appiconset/$iphone_name
}

iPadIconWithSize() {
    ipad_array=($(echo $1 | tr 'x' ' '))
    ipad_left="${ipad_array[0]}"
    ipad_right="${ipad_array[1]}"
    ipad_size=$(echo "$ipad_left * $ipad_right" | bc)
    ipad_name=icon-${ipad_left}x${ipad_right}.png
    if [ $ipad_right = '1' ]; then
        ipad_name=icon-${ipad_left}-ipad.png
    else
        ipad_name=icon-${ipad_left}@${ipad_right}x-ipad.png
    fi
    sips -Z $ipad_size $icon_path --out ./AppIcon.appiconset/$ipad_name
}

appStoreWithSize() {
    sips -Z $1 $icon_path --out ./AppIcon.appiconset/icon-1024.png
}
#启动页背景
launchBGWithSize() {
    sips -Z $1 $icon_path --out ./AppIcon.appiconset/appStore.png
}

#applog
appLogoStoreWithSize() {
    appLogo_array=($(echo $1 | tr 'x' ' '))
    appLogo_left="${appLogo_array[0]}"
    appLogo_right="${appLogo_array[1]}"
    appLogo_size=$(expr $appLogo_left \* $appLogo_right)
    appLogo_name=applogo@${appLogo_right}x.png
    sips -Z $appLogo_size $icon_path --out ./applogo.imageset/$appLogo_name
}

#


setContents() {
    cat <<EOF >./AppIcon.appiconset/Contents.json
{
    "images": [
        {
            "size": "20x20",
            "idiom": "iphone",
            "filename": "icon-20@2x.png",
            "scale": "2x"
        },
        {
            "size": "20x20",
            "idiom": "iphone",
            "filename": "icon-20@3x.png",
            "scale": "3x"
        },
        {
            "size": "29x29",
            "idiom": "iphone",
            "filename": "icon-29.png",
            "scale": "1x"
        },
        {
            "size": "29x29",
            "idiom": "iphone",
            "filename": "icon-29@2x.png",
            "scale": "2x"
        },
        {
            "size": "29x29",
            "idiom": "iphone",
            "filename": "icon-29@3x.png",
            "scale": "3x"
        },
        {
            "size": "40x40",
            "idiom": "iphone",
            "filename": "icon-40@2x.png",
            "scale": "2x"
        },
        {
            "size": "40x40",
            "idiom": "iphone",
            "filename": "icon-40@3x.png",
            "scale": "3x"
        },
        {
            "size": "57x57",
            "idiom": "iphone",
            "filename": "icon-57.png",
            "scale": "1x"
        },
        {
            "size": "57x57",
            "idiom": "iphone",
            "filename": "icon-57@2x.png",
            "scale": "2x"
        },
        {
            "size": "60x60",
            "idiom": "iphone",
            "filename": "icon-60@2x.png",
            "scale": "2x"
        },
        {
            "size": "60x60",
            "idiom": "iphone",
            "filename": "icon-60@3x.png",
            "scale": "3x"
        },
        {
            "size": "20x20",
            "idiom": "ipad",
            "filename": "icon-20-ipad.png",
            "scale": "1x"
        },
        {
            "size": "20x20",
            "idiom": "ipad",
            "filename": "icon-20@2x-ipad.png",
            "scale": "2x"
        },
        {
            "size": "29x29",
            "idiom": "ipad",
            "filename": "icon-29-ipad.png",
            "scale": "1x"
        },
        {
            "size": "29x29",
            "idiom": "ipad",
            "filename": "icon-29@2x-ipad.png",
            "scale": "2x"
        },
        {
            "size": "40x40",
            "idiom": "ipad",
            "filename": "icon-40.png",
            "scale": "1x"
        },
        {
            "size": "40x40",
            "idiom": "ipad",
            "filename": "icon-40@2x.png",
            "scale": "2x"
        },
        {
            "size": "50x50",
            "idiom": "ipad",
            "filename": "icon-50.png",
            "scale": "1x"
        },
        {
            "size": "50x50",
            "idiom": "ipad",
            "filename": "icon-50@2x.png",
            "scale": "2x"
        },
        {
            "size": "72x72",
            "idiom": "ipad",
            "filename": "icon-72.png",
            "scale": "1x"
        },
        {
            "size": "72x72",
            "idiom": "ipad",
            "filename": "icon-72@2x.png",
            "scale": "2x"
        },
        {
            "size": "76x76",
            "idiom": "ipad",
            "filename": "icon-76.png",
            "scale": "1x"
        },
        {
            "size": "76x76",
            "idiom": "ipad",
            "filename": "icon-76@2x.png",
            "scale": "2x"
        },
        {
            "size": "83.5x83.5",
            "idiom": "ipad",
            "filename": "icon-167.png",
            "scale": "2x"
        },
        {
            "size": "1024x1024",
            "idiom": "ios-marketing",
            "filename": "icon-1024.png",
            "scale": "1x"
        }
    ],
    "info": {
        "version": 1,
        "author": "icon.wuruihong.com"
    }
}
EOF
}

setAppLogoContents() {
    cat <<EOF >./applogo.imageset/Contents.json
{
    "images" : [
        {
            "idiom" : "universal",
            "filename" : "applogo@1x.png",
            "scale" : "1x"
        },
        {
            "idiom" : "universal",
            "filename" : "applogo@2x.png",
            "scale" : "2x"
        },
        {
            "idiom" : "universal",
            "filename" : "applogo@3x.png",
            "scale" : "3x"
        }
    ],
    "info" : {
        "version" : 1,
        "author" : "xcode"
    }
}
EOF
}

cd './ios/Runner/Assets.xcassets/'
rm -rf AppIcon.appiconset
mkdir AppIcon.appiconset
setContents

rm -rf applogo.imageset
mkdir applogo.imageset
setAppLogoContents


for iPhoneSize in "20x2" "20x3" "29x1" "29x2" "29x3" "40x1" "40x2" "40x3" "50x1" "50x2" "57x1" "57x2" "60x2" "60x3" "72x1" "72x2" "76x1" "76x2" "167x1" "1024x1"; do
    echo 'make iPhone icon:'$iPhoneSize
    iPhoneIconWithSize $iPhoneSize
done

for iPadSize in "20x1" "20x2" "29x1" "29x2"; do
    echo 'make iPad icon:'$iPadSize
    iPadIconWithSize $iPadSize
done

for applogoSize in "60x1" "60x2" "60x3"; do
    echo 'make applogo image:'$applogoSize
    appLogoStoreWithSize $applogoSize
done


cd ../../../